package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import app.server.project.main.service.service.recharge.PayStatus;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.util.Date;

@Data
@Entity(name = "recharge_order")
@TableName("recharge_order")
@Comment(value = "充值订单表")
@SynSpeed
public class RechargeOrderEntity {


    @Id
    @Column(name = "id")
    @Comment("订单id")
    @TableId("id")
    private Long id;


    @Column(name = "user_id")
    @Comment("用户id")
    @TableField("user_id")
    private long userId;


    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private Date createTime;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    @TableField("recharge_cfg_id")
    @Comment("订单类型")
    @Column(name = "recharge_cfg_id")
    private int rechargeCfgId;

    @TableField("pay_type")
    @Comment("支付渠道")
    @Column(name = "pay_type")
    private int payType;

    @TableField("status")
    @Comment("status 0 待支付 1 已支付")
    @Column(name = "status")
    private int status;
    @Column(name = "consume_num")
    @Comment("消费金额")
    @TableField("consume_num")
    private long consumeNum;

    public static RechargeOrderEntity of(long id, long userId, int rechargeCfgId, int payType, long consumeNum) {
        var ret = new RechargeOrderEntity();
        ret.setId(id);
        ret.setSynDb(true);
        ret.setCreateTime(new Date());
        ret.setUserId(userId);
        ret.setPayType(payType);
        ret.setConsumeNum(consumeNum);
        ret.setRechargeCfgId(rechargeCfgId);
        ret.setStatus(PayStatus.Wait);
        return ret;
    }

    public static RechargeOrderEntity of(long id) {
        var ret = new RechargeOrderEntity();
        ret.setId(id);
        return ret;
    }

    public void setConsumeNum(long consumeNum) {
        this.consumeNum = consumeNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getConsumeNum, this);
    }

    public void setPayType(int payType) {
        this.payType = payType;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getPayType, this);
    }

    public void setRechargeCfgId(int rechargeCfgId) {
        this.rechargeCfgId = rechargeCfgId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getRechargeCfgId, this);
    }


    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getUserId, this);
    }


    public void setStatus(int status) {
        this.status = status;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getStatus, this);
    }


    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RechargeOrderEntity::getCreateTime, this);
    }
}
